﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using Simple.Services.System.QHSEUser.Models;
using Simple.Services.System.QHSEViolationCategory.Models;

namespace Simple.Services;

public class QHSEViolationClauseAssocitionService
{
    private readonly SimpleDbContext _context;
    private readonly QHSEViolationCategoryService _categoryService;
    private readonly QHSEViolationCategoryHSEService _violationCategoryHSEService;
    private readonly QHSEViolationCategoryObsService _violationCategoryObsService;
    private readonly QHSEXyViolationTypeService _violationCategoryXyViolationTypeService;
    public QHSEViolationClauseAssocitionService(SimpleDbContext context, QHSEViolationCategoryService categoryService, QHSEViolationCategoryHSEService violationCategoryHSEService, QHSEViolationCategoryObsService violationCategoryObsService, QHSEXyViolationTypeService violationCategoryXyViolationTypeService)
    {
        _context = context;
        _categoryService = categoryService;
        _violationCategoryHSEService = violationCategoryHSEService;
        _violationCategoryObsService = violationCategoryObsService;
        _violationCategoryXyViolationTypeService = violationCategoryXyViolationTypeService;
    }
    public async Task<List<QHSEViolationClauseAssocitionModel>> GetAsync()
    {
        var violationCategories = await _context.Set<QHSESysViolationClauseAssocition>().ToListAsync();
        return MapperHelper.Map<List<QHSEViolationClauseAssocitionModel>>(violationCategories);
    }
    public async Task<ClauseResultModel> GetClauseResult(IdInputModel idInput)
    {
        var clauseAssocition = await _context.Set<QHSESysViolationClauseAssocition>()
            .AsNoTracking()
            .FirstOrDefaultAsync(u=>idInput.Id.Equals(u.ClauseId));

        if (clauseAssocition == null)
        {
            return null;
        }

        var category = await _categoryService.GetByCodeAsync(clauseAssocition.ViolationCategoryCode);
        var hseResultTask = _violationCategoryHSEService.GetResult(new QHSEViolationCategoryHSEPageInputModel { Id = category.Id });
        var obsResultTask = _violationCategoryObsService.GetResult(new QHSEViolationCategoryObsPageInputModel { Id = category.Id });
        var violationType = await _violationCategoryXyViolationTypeService.GetByCode(clauseAssocition.ViolationTypeCode);

        await Task.WhenAll(hseResultTask, obsResultTask);

        var clauseResultModel = new ClauseResultModel
        {
            ViolationCategoryId = category.Id,
            ViolationCategory = category.Item,
            HSE = hseResultTask.Result.Result,
            Observe = obsResultTask.Result.Result,
            ViolationType = violationType.ViolationType,
            ViolationTypeCode =clauseAssocition.ViolationTypeCode,
            Fine = (float)violationType.Fine,
            score = (float)violationType.Score
        };

        clauseResultModel.ViolationTypeCategory = await _context.Set<QHSESysDictionary>().AsNoTracking().Where(u => u.Code == clauseAssocition.ClauseTypeCode).Select(u => u.Item).FirstOrDefaultAsync();

        return clauseResultModel;
    }

    public async Task<QHSEXyViolationClauseModel> GetByIdAsync(IdInputModel model)
    {
        var clause = await _context.Set<QHSESysXyViolationClause>().AsNoTracking().Where(u => u.GeneralSpecialtyId == model.Id).Join(_context.Set<QHSESysKeyWorkSpecialty>(),
            clause => clause.SpecialtyCode,
            specialty => specialty.SpecialtyCode,
            (clause, specialty) => new { Clause = clause, Specialty = specialty })
            .Select(join => new QHSESysDangerClause
            {
                GeneralSpecialtyId = join.Clause.GeneralSpecialtyId,
                SpecialtyCode = join.Clause.SpecialtyCode,
                DangerTypeCode = join.Clause.ViolationTypeCode,
                Description = "(" + join.Specialty.Specialty + ")" + join.Clause.No + join.Clause.Description,
                Note = join.Clause.Note,
                No = join.Clause.No
            }
            ).FirstOrDefaultAsync();
        var models = MapperHelper.Map<QHSEXyViolationClauseModel>(clause);

        return models;
    }

}
